package com.ymatou.query.seller.trading.api.parameter.sp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * [sp_trd_GetPackageDeliveryOrdersCount]
 * 
 * @author songyefeng
 *
 */
public class Sp_GetPackageDeliveryOrdersCount {
	private Integer sellerId;
	private Boolean domesticDelivered;
	private String timeFrom;
	private String timeTo;

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public void setDomesticDelivered(Boolean domesticDelivered) {
		this.domesticDelivered = domesticDelivered;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	@SuppressWarnings("rawtypes")
	public List<Map> execute() {
		List<Map> t = new ArrayList<Map>();
		DefaultWapper wapper = new DefaultWapper();
		String sqlCmd = "select distinct o.iOrderId from ymt_orders o with(nolock,index=idx_Ymt_Orders_iBuyerId_iTradingStatus_bDomesticDelivered)"
				//砍价团订单，在组团成功前，结果不展示。add by sean 03.09
				+ " LEFT JOIN dbo.Ymt_OrderBiz b ON b.OrderId = o.iOrderId join ymt_orderinfo i with(nolock,index=idx_Ymt_OrderInfo_iCatalogStatus)"
				+ " on o.iorderid = i.iorderid and i.iCatalogStatus = 7"
				+ " where ( b.OrderId IS NULL  OR b.BizStatus =3) and o.iBuyerId = "
				+ sellerId + " and o.iTradingStatus = 3";
		if (null == domesticDelivered) {
			t = wapper.executeSql(sqlCmd);
			List orders = t.stream().map(x -> x.get("iOrderId"))
					.collect(Collectors.toList());
			if (false == DataUtil.isNullOrEmpty(orders)) {
				return getResult(orders);
			}
		} else if (false == domesticDelivered) {
			sqlCmd += " and o.bDomesticDelivered is NULL";
			t = wapper.executeSql(sqlCmd);
			List orders = t.stream().map(x -> x.get("iOrderId"))
					.collect(Collectors.toList());
			if (false == DataUtil.isNullOrEmpty(orders)) {
				return getResult(orders);
			}
		} else {
			sqlCmd += " and o.bDomesticDelivered = 1";
			t = wapper.executeSql(sqlCmd);
			List orders = t.stream().map(x -> x.get("iOrderId"))
					.collect(Collectors.toList());
			if (false == DataUtil.isNullOrEmpty(orders)) {
				return getResult(orders);
			}
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	private List<Map> getResult(List orders) {
		StringBuffer sb = new StringBuffer();
		sb.append("select bShangouOrder as shangou, count(1) as [count] ");
		sb.append(" from Ymt_Orders with (nolock,index = PK_Ymt_Orders) where iOrderId in "
				+ DataUtil.toInOfSql(orders));
		if (null != timeFrom) {
			sb.append(" and dAddTime >= '" + timeFrom + "'");
		}
		if (null != timeTo) {
			sb.append(" and dAddTime < '" + timeTo + "'");
		}
		sb.append(" group by bShangouOrder");
		return new DefaultWapper().executeSql(sb.toString());
	}

	public static void main(String[] args) {
		Sp_GetPackageDeliveryOrdersCount sp = new Sp_GetPackageDeliveryOrdersCount();
		sp.setSellerId(3383);
		sp.setDomesticDelivered(false);
		sp.setTimeFrom(YMTDateUtil.getBeforeOrNextDay(-190));
		sp.setTimeTo(YMTDateUtil.getBeforeOrNextDay(2));
		System.out.println(sp.execute());
	}
}
